﻿using System;
using System.Collections.Generic;
using Core.Logger;
using Xunit;

namespace UnitTest
{
    public class LoggerUnitTest
    {
        public LoggerUnitTest()
        {
            LoggerInstance.Clear();
        }

        [Fact]
        public void FirstTest()
        {
            LoggerInstance.Append("first line");
            LoggerInstance.Append("second line");

            Assert.Equal("first line", LoggerInstance.GetLogs()[0].Message);
            Assert.Equal("second line", LoggerInstance.GetLogs()[1].Message);
        }

        [Fact]
        public void LogEventTest()
        {
            byte count = 0;
            LoggerInstance.AfterAppend += delegate { count = 5; };
            LoggerInstance.Append("new log");
            Assert.Equal(5, count);
        }

        [Fact]
        public void DefaultLogStatus_ExpectsInfo()
        {
            LoggerInstance.Append("Default log status is info");
            Assert.Equal(LogStatus.Info, LoggerInstance.GetLogs()[0].Status);
        }

        [Fact]
        public void ClearLog_ExpectNoItems()
        {
            LoggerInstance.Append("first line");
            LoggerInstance.Append(LogStatus.Error, "second line");

            Assert.True(LoggerInstance.GetLogs().Count == 2);

            LoggerInstance.Clear();
            Assert.True(LoggerInstance.GetLogs().Count == 0);
        }

        [Fact]
        public void ErrorMessages_ReturnsErrors()
        {
            LoggerInstance.Append("first info message");
            LoggerInstance.Append(LogStatus.Error, "first error message");
            LoggerInstance.Append(LogStatus.Error, "second error message");
            LoggerInstance.Append("second infor message");

            Assert.Equal(2, LoggerInstance.GetErrors().Count);
            Assert.Equal("first error message", LoggerInstance.GetErrors()[0]);
            Assert.Equal("second error message", LoggerInstance.GetErrors()[1]);
        }

        [Fact]
        public void WarningMessages_ReturnsWarnings()
        {
            LoggerInstance.Append("first info");
            LoggerInstance.Append(LogStatus.Warning, "first warning");
            LoggerInstance.Append(LogStatus.Error, "first error");
            LoggerInstance.Append(LogStatus.Warning, "second warning");

            List<string> warnings = LoggerInstance.GetWarnings();

            Assert.Equal(2, warnings.Count);
            Assert.Equal("first warning", warnings[0]);
            Assert.Equal("second warning", warnings[1]);
        }

        [Fact]
        public void FormatMessge()
        {
            LoggerInstance.Append("Format param1: {0}", 58);

            Assert.Equal("Format param1: 58", LoggerInstance.GetLogs()[0].Message);
        }

        [Fact]
        public void FormatMessage_ExpectedDefaultInfoStatus()
        {
            LoggerInstance.Append("Default value should be info: {0}, second value {1}", "inf", 33);

            Console.WriteLine(LoggerInstance.GetLogs()[0]);

            Assert.Equal(LoggerInstance.GetLogs()[0].Status, LogStatus.Info);
        }
    }
}